package com.hive.jdbc.impl;

import com.hive.jdbc.JdbcService;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import org.apache.calcite.adapter.jdbc.JdbcSchema;
import org.apache.calcite.jdbc.CalciteConnection;
import org.apache.calcite.schema.Schema;
import org.apache.calcite.schema.SchemaPlus;
import org.datanucleus.store.rdbms.datasource.dbcp.BasicDataSource;

/**
 * ClassName HIveJdbcServiceImpl Description TODO
 *
 * @author qq
 * @date 2023/7/28 11:22
 * @since JDK 1.8
 */
public class HIveJdbcServiceImpl implements JdbcService {

  @Override
  public Statement getStatement()
    {
      Properties info = new Properties();
      info.setProperty("lex", "JAVA");
      CalciteConnection calciteConnection = null;
      Statement stat = null;
      try {
        Class.forName("org.apache.calcite.jdbc.Driver");
        Connection connection =
            DriverManager.getConnection("jdbc:calcite:", info);
        calciteConnection =
            connection.unwrap(CalciteConnection.class);
        SchemaPlus rootSchema = calciteConnection.getRootSchema();
        //创建Mysql的数据源schema
        Class.forName("org.apache.hive.jdbc.HiveDriver");
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl(
            "jdbc:hive2://192.168.72.102:10000/hive_1");
        dataSource.setUsername("root");
//        dataSource.setPassword("123456");
        Schema schema = JdbcSchema.create(rootSchema, "hive_1", dataSource,
            "hive_1", "hive_1");
        rootSchema.add("hive_1", schema);
        stat = calciteConnection.createStatement();
      } catch (ClassNotFoundException e) {
        e.printStackTrace();
      } catch (SQLException e) {
        e.printStackTrace();
      }
      return stat;
    }
}
